var address = require('../../utils/city.js')
import network from '../../utils/network.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mendianId:0,
    mendianItems:[
      
    ],
    mendianIsShow:false,
    id:0,
    currentTab:0,
    areaAddress:'请选择收货地址',
    animationAddressMenu: {},
    animationAddressMenuMendian: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    areaInfo: '',
    goodsImg:'',
    goodsName:'',
    goodsStarNum:0,
    showSuc:false,
    showFail:false,
    addressDetail:'',
    userName:'',
    userPhone:'',
  },
  closeFail:function(){
    this.setData({
      showFail:false
    })
  },
  closeSuc: function () {
    this.setData({
      showSuc: false
    })
  },
  getSupport:function(){
    var scene = "?url=" + this.data.goodsImg + "&uid=" + app.globalData.userInfo.id + "&name=" + app.globalData.userInfo.nickname + "&doll=" + app.globalData.userInfo.doll
    wx.navigateTo({
      url: '../supportShare/supportShare' + scene,
    })
  },

  chooseWay:function(e){
    var that = this
    // var animation = wx.createAnimation({
    //   duration: 100,
    //   timingFunction: 'ease',
    // })
    // var isShow = false
    // if (isShow) {
    //   // vh是用来表示尺寸的单位，高度全屏是100vh
    //   animation.translateY(0 + 'vh').step()
    // } else {
    //   animation.translateY(40 + 'vh').step()
    // }
    
    
    // if (this.data.currentTab == 0) {
    //   that.setData({
    //     animationAddressMenu: animation.export(),
    //     addressMenuIsShow: isShow,
    //   })
    // } else {
    //   that.setData({
    //     animationAddressMenuMendian: animation.export(),
    //     mendianIsShow: isShow,
    //   })
    // }
    var index = e.currentTarget.dataset['index'];
    this.setData({
      currentTab:index
    })
    if(index == 0){
      this.setData({
        areaAddress: '请选择收货地址'
      })
    }else{
      this.setData({
        areaAddress: '请选择门店'
      })
    }
    
  },
  addressDetailInput:function(e){
    this.setData({
      addressDetail: e.detail.value
    })

  },
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })

  },
  userPhoneInput: function (e) {
    this.setData({
      userPhone: e.detail.value
    })

  },
  gotoExchange:function(){
    if(this.data.currentTab==0){
    if (this.data.areaInfo.length==0){
      wx.showToast({ title: '请选择收货区域', icon: 'none' })
      return
    }
    if (this.data.addressDetail.length == 0){
      wx.showToast({ title: '请填写收货详细地址', icon: 'none' })
      return
    }
    if (this.data.userName.length == 0) {
      wx.showToast({ title: '请填写收件人', icon: 'none' })
      return
    }
    if (this.data.userPhone.length == 0) {
      wx.showToast({ title: '请填写收件人联系电话', icon: 'none' })
      return
    }
    }else{
      if (this.data.areaInfo.length == 0) {
        wx.showToast({ title: '请选择门店', icon: 'none' })
        return
      }
    }

    this.requstExchange()

  },

  requstExchange:function(){
    let that = this
    network.post({
      url: 'orders', data: {
        goods_id: that.data.id, // 商品id,必填
        exchange_type: that.data.currentTab==0?1:0, // 兑换类型 1：线上 0： 线下 必填
        branch_id: that.data.currentTab == 0 ? 0 : this.data.mendianId, // 分店ID 当选择线下兑换时必填
        address:{// 收件人地址信息 当选择线上兑换时必填
          name: that.data.userName, // 收件人名称
          phone: that.data.userPhone, // 收件人电话
          address: that.data.areaInfo + that.data.addressDetail, // 地址信息
        }

      },
    })
      .then(({ code, data }) => {
        
        if (code == 0) {
          that.showSucArea()
        } else if(code==-10012) {
          that.showFailArea()
        }else{
          wx.showToast({ title: '系统错误', icon: 'none' })
        }
        that.getUser()

      })
  },
  getUser:function(){
    let that = this
    network.get({
      url: 'auth/me'
    })
      .then(({ code, data }) => {
        console.log("data")
        console.log(data)
        if (code == 0) {
          if (data != null) {
            app.globalData.userInfo = data
          }

        }

      })
  },
  showFailArea: function () {
    this.setData({ showFail: true })
  },

  showSucArea:function(){
    this.setData({showSuc:true})
  },

  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    // 如果已经显示，不在执行显示动画
    if(this.data.currentTab == 0){
      console.log(that.data.addressMenuIsShow)
    if (that.data.addressMenuIsShow) {
      return
    }
    }else{
      if (that.data.mendianIsShow) {
        return
      }
      this.setData({
        mendianId: this.data.mendianItems[0].id,
        areaAddress: this.data.mendianItems[0].name,
        areaInfo: this.data.mendianItems[0].name
      })
    }
    
    // 执行显示动画
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease',
    })
    if (isShow) {
      // vh是用来表示尺寸的单位，高度全屏是100vh
      animation.translateY(0 + 'vh').step()
    } else {
      animation.translateY(40 + 'vh').step()
    }
    console.log(this.data.currentTab)
    if(this.data.currentTab == 0){
    that.setData({
      animationAddressMenu: animation.export(),
      addressMenuIsShow: isShow,
    })
    }else{
      that.setData({
        animationAddressMenuMendian: animation.export(),
        mendianIsShow: isShow,
      })
    }
  
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
      areaAddress:areaInfo
    })
  },

  mendianSure:function(){
    // var value = e.detail.value[0]
    // this.setData({
    //   mendianId: this.data.mendianItems[value].id,
    //   areaAddress: this.data.mendianItems[value].name,
    //   areaInfo: this.data.mendianItems[value].name
    // })
  },
  // 点击蒙版时取消组件的显示
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      // 滑动选择了区
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },

  mendianChange:function(e){
    console.log(e)
    var value = e.detail.value[0]
    this.setData({
      mendianId: this.data.mendianItems[value].id,
      areaAddress: this.data.mendianItems[value].name,
      areaInfo: this.data.mendianItems[value].name
    })

  },

  getMendianList:function(){
    let that = this
    network.get({
      url: 'branches',data:{
        pagination: { // 分页参数 如果不传返回全部
          page: 1, // 当前页
          per_page: 50 // 每页条数 默认10
        },
      }
    })
      .then(({ code, data }) => {
        console.log("data")
        console.log(data)
        if (code == 0) {
          if (data.list && data.list.length > 0) {
            that.setData({ mendianItems: data.list })
          }
        }

      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 默认联动显示北京
    this.setData({
      id: options.id,
      goodsImg: options.pic,
      goodsName:options.name,
      goodsStarNum:options.price
    })
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
    this.getMendianList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var scene = "?url=" + this.data.goodsImg + "&uid=" + app.globalData.userInfo.id + "&name=" + app.globalData.userInfo.nickname + "&doll=" + app.globalData.userInfo.doll
    return {
      title: app.globalData.userInfo.nickname + "想要兑换娃娃，快来帮帮TA!",
      path: 'pages/index/index' + scene,
      imageUrl:'../image/fenxiang.png'
    }
  }
})